package com.ma4567.remotecontroller.VideoRequest;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.ma4567.remotecontroller.MysqlHelper.TableLogManager;
import com.ma4567.remotecontroller.MysqlHelper.TableRequestLog;
import com.ma4567.remotecontroller.RemoteControllerApplication;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;

import java.awt.*;
import java.sql.SQLException;

@CrossOrigin
@RestController
public class MouseMove {
    public static Robot robot;
    @RequestMapping(value = "/api/moveMouse", method = RequestMethod.GET)
    public void controlMouseMove(HttpServletRequest request,
                                 @RequestParam("cookie") String Cookie,
                                 @RequestParam("offsetX") float offsetX,
                                 @RequestParam("offsetY") float offsetY,
                                 @RequestParam("elementX") float elementX,
                                 @RequestParam("elementY") float elementY) throws SQLException {
        TableRequestLog.AddLog("Control mouse", Cookie, request.getRemoteAddr());

        if(RemoteControllerApplication.userManager.JudgeCookie(Cookie)){//鉴cookie
            if(RemoteControllerApplication.userManager.GetUserSingleItemPermission(Cookie, "RemoteControl")){//鉴权
                float x = 1920 * (offsetX / elementX);
                float y = 1080 * (offsetY / elementY);

                try {
                    if(robot == null) robot = new Robot();
                    robot.mouseMove((int) x, (int) y);
                } catch (AWTException e) {
                    e.printStackTrace();
                }

            }
        }
    }
}
